package com.android.clinic.app.ui.doctor;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import com.android.clinic.app.R;
import com.android.clinic.app.base.BaseActivity;
import com.android.clinic.app.entity.ReportInfo;
import com.android.clinic.app.glide.ImageLoaderUtils;
import com.android.clinic.app.ui.personal.fragment.ImagePagerActivity;
import com.android.clinic.app.utils.Utils;
import java.util.Arrays;

/**
 * 我的报告详情
 * Created by cheng on 2018/7/17.
 */
public class MyReportDetailActivity extends BaseActivity {

    private String[] imageArray;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_report_detail;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ReportInfo reportInfo = (ReportInfo) this.getIntent().getSerializableExtra("reportInfo");
        setTitleBarTitle(reportInfo.getTitle());
        setTitleBarLeftResource(R.drawable.icon_back);

        TextView tvContent = (TextView) findViewById(R.id.tv_content);
        TextView tvTime = (TextView) findViewById(R.id.tv_time);
        GridView gridView = (GridView) findViewById(R.id.gridView);

        tvContent.setText(reportInfo.getContent());
        tvTime.setText(Utils.getFormatTime(reportInfo.getConnectTime()));

        if (!Utils.isEmpty(reportInfo.getImageUrl())) {
            imageArray = reportInfo.getImageUrl().split(",");
            ReportAdapter reportAdapter = new ReportAdapter();
            gridView.setAdapter(reportAdapter);
            gridView.setOnItemClickListener((parent, view, position, id) -> {
                ImagePagerActivity.setLists(Arrays.asList(imageArray));
                Intent intent = new Intent(MyReportDetailActivity.this, ImagePagerActivity.class);
                intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
                startActivity(intent);
                overridePendingTransition(0, 0);
            });
        }
    }

    private class ReportAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return imageArray.length;
        }

        @Override
        public String getItem(int position) {
            return imageArray[position];
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (null == convertView) {
                convertView = LayoutInflater.from(MyReportDetailActivity.this).inflate(R.layout.item_grid_report, null);
                holder = new ViewHolder();
                holder.ivItemIcon = (ImageView) convertView.findViewById(R.id.iv_report_icon);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            ImageLoaderUtils.getInstance().loadNormalImage(imageArray[position], holder.ivItemIcon);
            return convertView;
        }

        class ViewHolder {
            ImageView ivItemIcon;
        }
    }

}
